package com.dorukdestan.p2pshare.logger;

public class Logger {
	public static boolean active;
	
	/**
	 * mode[0] info
	 * mode[1] warning
	 * mode[2] error
	 * mode[3] debug
	 */
	private static boolean[] mode;
	
	static{
		active = true;
		mode = new boolean[4];
		mode[0] = true;
		mode[1] = true;
		mode[2] = true;
		mode[3] = true;
	}
	private Class<?> klazz;
	
	public Logger(Class<?> klazz) {
		this.klazz = klazz;
	}
	
	public synchronized static void changeMod(boolean info, boolean warning, boolean error, boolean debug){
		mode[0] = info;
		mode[1] = warning;
		mode[2] = error;
		mode[3] = debug;
	}
	
	public synchronized static void toggle(boolean isActive){
		active = isActive;
	}

	public synchronized void info(String str){
		if(active && mode[0])
			System.out.println("INFO:" + getClassName() + str);
	}
	
	public synchronized void warning(String str){
		if(active && mode[1])
			System.out.println("WARNING:" + getClassName() + str);
	}
	
	public synchronized void error(String str){
		if(active && mode[2])
			System.out.println("ERROR:" + getClassName() + str);
	}
	
	public synchronized void error(Throwable t){
		if(active && mode[2]){
			System.out.println("ERROR:" + getClassName() + t.getMessage());
			t.printStackTrace();
		}
	}
	
	public synchronized void error(String str, Throwable t){
		if(active && mode[2]){
			System.out.println("ERROR:" + getClassName() + str);
			System.out.println("ERROR:" + getClassName() + t.getMessage());
		}
	}
	
	public synchronized void debug(String str){
		if(active && mode[3])
			System.out.println("DEBUG:" + getClassName() + str);
	}
	
	private String getClassName(){
		return " <" + klazz.getSimpleName() + "> ";
	}
}
